package com.hyts.standard.webflux.meter;

import io.micrometer.core.instrument.LongTaskTimer;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

public class Timers {

  private static SimpleMeterRegistry registry = new SimpleMeterRegistry();

  public void record() {
    Timer timer = registry.timer("simple");
    for(int i = 0 ; i < 6;i++) {
      timer.record(() -> {
        try {
          Thread.sleep(3000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      });
    }
  }

  public void sample() {
    Timer.Sample sample = Timer.start();
    new Thread(() -> {
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      sample.stop(registry.timer("sample"));
    }).start();
  }

  public void longTask() {
    LongTaskTimer timer = registry.more().longTaskTimer("long");
    timer.record(() -> {
      try {
        Thread.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    });
  }

  public static void main(String[] args){
    Timers timers = new Timers();
    timers.sample();
   System.out.println(registry.timer("simple").count());
  }

}
